# encoding = utf-8

import pygame
from pygame.sprite import Sprite


class Snake(Sprite):
    """表示蛇的类"""

    def __init__(self,screen):
        super().__init__()
        self.screen = screen
        #参数2为颜色设置，参数3依次为矩形距离上边缘、左边缘、下边缘、右边缘，参数4为边界宽度
        self.image = pygame.image.load('images/snake1.jpg')
        self.image = pygame.transform.scale(self.image,(10,10))
        self.rect = self.image.get_rect()

        self.rect.centerx = self.screen.get_rect().centerx
        self.rect.centery = self.screen.get_rect().centery

        self.centerx = float(self.rect.centerx)
        self.centery = float(self.rect.centery)
        self.state = True
        
    def update(self,direction):
        if direction == 'left':
            self.centerx = self.centerx - 10
            self.rect.centerx = self.centerx
            if self.rect.x < 0 :
                self.state = False
                
        elif direction == 'right':
            self.centerx = self.centerx + 10
            self.rect.centerx = self.centerx
            if  self.rect.right >= self.screen.get_rect().right:
                self.state = False
        elif direction == 'up' :
            self.centery = self.centery - 10
            self.rect.centery = self.centery
            if self.rect.y < 0:
                self.state = False
        elif direction == 'down' :
            self.centery = self.centery + 10
            self.rect.centery = self.centery
            if self.rect.bottom >= self.screen.get_rect().bottom:
                self.state = False
         
    def blitme(self):
        '''在指定位置绘制飞船'''
        self.screen.blit(self.image,self.rect)
            
    


